package com.dk.service.impl;

import com.dk.mapper.MenuMapper;
import com.dk.mapper.RoleMenuMapper;
import com.dk.pojo.Menu;
import com.dk.service.MenuService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@DubboService
@Service
public  class MenuServiceImpl implements MenuService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private RoleMenuMapper roleMenuMapper;

    @Autowired
    private MenuMapper menuMapper;

    @Override
    public List<Map<String, Object>> getInfo(Integer pid) {

        String key = "permission";

        String feild = "menu:info:" + pid;

        HashOperations opsForHash = redisTemplate.opsForHash();//hash方式

        Boolean b = opsForHash.hasKey(key, feild); //feild字段是否存在
        //判断是否存在 true存在 false不存在
        if(b){//存在

            List<Map<String, Object>> menuList =(List<Map<String, Object>>) opsForHash.get(key, feild);//获取

            return menuList;
        }else{ //不存在

            List<Menu> menuList = menuMapper.getInfoById(pid); //查询数据库

            List<Map<String, Object>> menuTree = getMenuTree(menuList, 0);

            opsForHash.put(key,feild,menuTree); //增加redis到缓存

            return menuTree;
        }
    }

    @Override
    public List<Map<String, Object>> getMenuZtree() {

        List<Menu> list = menuMapper.selectByExample(null);

        List<Map<String,Object>> mapList = getMenuZtree(list,0);

        return mapList;
    }

    @Override
    public boolean addInfo(Menu menu) {

        return menuMapper.insertSelective(menu) > 0;
    }

    @Override
    public boolean deleteInfo(Integer id) {

        return menuMapper.deleteByPrimaryKey(id) > 0;
    }

    @Override
    public Menu selectById(Integer id) {

        return menuMapper.selectByPrimaryKey(id);
    }

    @Override
    public boolean updateMenuInfo(Menu menu) {

        return menuMapper.updateByPrimaryKeySelective(menu) > 0;
    }

    @Override
    public List<Map<String, Object>> getMenuZtreeChecked(Integer roleId) {

        List<Menu> list =  menuMapper.selectByExample(null);

        List<Integer> mids = roleMenuMapper.getMenuZtreeChecked(roleId);

        return getMenuZtreeUpdate(list,0,mids);
    }

    public List<Map<String,Object>> getMenuZtreeUpdate(List<Menu> list, Integer id,List<Integer> mids){

        List<Map<String,Object>> result = new ArrayList<>();
        //便利获取数据
        for (int i = 0; i < list.size(); i++) {

            Map<String ,Object> map = null;

            Menu menu = list.get(i);

            if (id == menu.getParentid()) {

                map = new HashMap<>();
                //拼接的树的属性
                map.put("name",menu.getText());

                map.put("id",menu.getId());
                //因为可能有子节点 然后 []里面有{} 和上面第一次的循环是一样的数据
                //递归调用自己
                map.put("children",getMenuZtreeUpdate(list,menu.getId(),mids));

                if (mids !=null && mids.size()>0 && mids.contains(menu.getId())){

                    map.put("checked", true);
                }
            }

            if (map !=null){

                List<Map<String,Object>> r = (List<Map<String, Object>>) map.get("children");

                if (r.size()<=0){

                    map.remove("children");
                }
                result.add(map);
            }
        }
        return result;
    }

    public List<Map<String,Object>> getMenuZtree(List<Menu> list, Integer id){
        //定义一个集合进行数据的接受
        List<Map<String,Object>> result = new ArrayList<>();
        //便利获取数据
        for (int i = 0; i < list.size(); i++) {

            Map<String ,Object> map = null;

            Menu menu = list.get(i);

            if (id == menu.getParentid()) {

                map = new HashMap<>();
                //拼接的树的属性
                map.put("name",menu.getText());

                map.put("id",menu.getId());
                //因为可能有子节点 然后 []里面有{} 和上面第一次的循环是一样的数据
                //递归调用自己
                map.put("children",getMenuZtree(list,menu.getId()));
            }

            if (map !=null){

                List<Map<String,Object>> r = (List<Map<String, Object>>) map.get("children");

                if (r.size()<=0){

                    map.remove("children");
                }
                result.add(map);
            }
        }
        return result;
    }

    private List<Map<String, Object>> getMenuTree(List<Menu> list, Integer id) {

        List<Map<String,Object>> result = new ArrayList<>();

        for (int i = 0; i < list.size();i++) {

            Map<String,Object> map = null;

            Menu menu = list.get(i);

            if (id == menu.getParentid()) {

                map = new HashMap<>();
                //拼接的树的属性
                map.put("text",menu.getText());

                map.put("url",menu.getUrl());

                map.put("id",menu.getId());
                //因为可能有子节点 然后 []里面有{} 和上面第一次的循环是一样的数据
                //递归调用自己
                map.put("nodes",getMenuTree(list,menu.getId()));
            }

            if (map !=null){

                List<Map<String,Object>> r = (List<Map<String, Object>>) map.get("nodes");

                if (r.size() <= 0){

                    map.remove("nodes");
                }else {

                    map.put("selectable", false);
                }
                result.add(map);
            }
        }
        return result;
    }

    @Override
    public List<String> selectPermsById(Integer pid) {

        return menuMapper.selectPermsById(pid);

    }
}
